#ifndef VPCONN_H
#define VPCONN_H

#include "progconn.h"
#include "GameDBInterface.h"

#include "vpproto.h"

class VPHand;
class VPokerOpts;
class VPokerSavedGame;
class ShueSave;
class VPGameBase;


// kharmon.  03-22-06.  #10515.  Changed base class so that this server 
// can support progressive games.
class VPConnection : public ProgressiveConnection
{
	public:
		VPConnection(ProgressiveServer* server, SOCKET client);
		~VPConnection();

		void msgStart();
		void msgAdd(uint8 Byte);
		void msgAdd(int32 SignedLong);
		uint8* msgGetBuf();
		void msgReleaseBuf(int32 Amt);
		void msgEnd();

        // what game type is currently active on this connection?
        VPGameID getGameID()                { return m_eGameID;             }

        bool8   getDoubleUpEnabled()        { return dblUpEnabled!=0;       }
		int32	getDoubleUpCapAmount()		{ return dlbUpCapAmount;		}
        bool8   getSideBetEnabled()         { return m_bSideBetEnabled;     }

        // currently only used by 7 stud poker.  Are we using a 52, 53, or 54 card deck?
        uint8   getDeckConfiguration()      { return m_ucDeckConfiguration; }

        // returns the protocol version currently being used to communicate with the client.
        uint8   getProtocolVersion1()       { return m_ucProtoVer1;         }
        uint8   getProtocolVersion2()       { return m_ucProtoVer2;         }

        bool8 isForMoney();
		bool8 shouldSaveGameData();

		enum 
		{
			MAX_SUBGAME_DATA=5
		};

		void	gameOver();

		//Signals from server to player
		void	Answer( uint8 ans );	//generic message
		void	Error( int err );		//error message

		int32	trackValidateLogin();
		int32	trackValidateSession();

		// CSN-7075: New Authentication
		void	trackLogoutSession();

	protected:
		bool8	getResponse();

		void	sendStopMsg();


		bool8	parseAnsiCasinoLogin( int32*);
		bool8	parseUnicodeCasinoLogin( int32*);
		bool8   parseReloadDenomination(int32*);	// CSN-13423: reload with a new denomination
		void	removeCasinoSpecific();

		int32	encode(uint8*, uint8*, int32);
		int32	decode(uint8*, uint8*, int32);

		bool8	trackGetBalance(int32 *);
		bool8	trackGetOptions(VPokerOpts*, uint8 machId, int32 creditValue);
		bool8	trackGetPlayerLimits(uint8 machId, int32* compFactor);
		bool8	trackHasSavedGame(int32* err);
		void	trackaddBadPacket(int32 error_reason, char MessageType, char *msgbuf, int32 msglen);

		void	HandlePlayerUnknown();	//Unknown
		void	HandlePlayerAnsiLogin	();	//Login
		void	HandlePlayerUnicodeLogin();	//login
		void    HandleReloadDenomination();	// CSN-13423: reload with a new denomination
		void	HandlePlayerLogin(bool isReload = false);

        // returns the appropriate database connection for the currently active game type.
        GameDBInterface * getGameDBInterface() { return m_pGameDBInterface; }

        virtual int32 getProgressiveFixedAmountMultiplier();    // Required by progressive base class.
        virtual bool8 getForRealFlag();                         // Required by progressive base class.

		void	allocateXMLTranslator( XMLTranslator** target );


	protected:
		enum Flags
		{
			LOGGEDON		= 0x01L,
			PAYTABLECHANGED	= 0x02L,
			DBLUPCHANGED	= 0x04L,
            SIDEBETPAYTABLECHANGED = 0x08L,
            SIDEBETENABLEDCHANGED  = 0x10L
		};

		uint32 flags;
		int32 machId;
		int32 creditValue;	// in cents: 25==quarter
		int32 payTableFreq;	// lo, med, hi
		int32 dblUpEnabled;
		int32 dlbUpCapAmount;
		int32 compFactor;
		VPGameBase* game;       // pointer to currently active game object.
		void* casinoSpecific;
		int32 msglen;
		uint8 msgbuf[256];

		uint8		subGameData[MAX_SUBGAME_DATA];
		int32		subGameLength;

        VPGameID        m_eGameID;              // identifies the current game type that this connection is servicing.

        GameDBInterface *m_pGameDBInterface;    // appropriate database connection for the currently active game type, created at login.

        bool8   m_bSideBetEnabled;              // If the current game has a sidebet, is it enabled?
        uint8   m_ucSideBetPayTable;            // If the current game has a sidebet, what paytable is used?
        uint8   m_ucDeckConfiguration;          // For games that support multiple deck configurations.

        uint8   m_ucProtoVer1;                  // major part of protocol version being used to communicate with client.
        uint8   m_ucProtoVer2;                  // minor part of protocol version being used to communicate with client.

        #ifdef FORCEDSHOE       //RPF 20100317 When forcing the shue now filter by pid
        String  m_pid;
        #endif

	public:
        int32 getCompFactor () { return compFactor; }
        int32 getCreditValue() { return creditValue; }
		int32 creditsToCents(int cred) { return cred*creditValue; }

		// truncates (floor) value since fractional credits not allowed
		//
		int centsToCredits(int cents) { return cents/creditValue; }

		uint8 getMachId() { return (uint8)(machId & 0xffL); }
        #ifdef FORCEDSHOE 
        String  GetPID() {return m_pid;}
        #endif
            
		int32 getPayTableFreq() { return payTableFreq; }

		int32 getMaxBet() { return 5; }		// currently, 5 credits max
		
		uint8* getSubGameData()   { return subGameData;    }
		int32  getSubGameLength() { return subGameLength;  }
		int32* getSubGameLengthByRef() { return &subGameLength; }


	// [AO 2008-01-16] Refactoring for HA
	private:
		bool ValidateSession();
};


// kharmon.  03-22-06.  #10515.  Used by progressives, required by base class.
inline
int32
VPConnection::getProgressiveFixedAmountMultiplier()
{
	// VP doesn't have a Fixed Amount Multiplier
	return 1;
}



#endif
